#define _CRT_SECURE_NO_WARNINGS 1

#include"Linc.h"

LinkNode* LinkBuyNode(L_Datype x)
{
	LinkNode* newnode = malloc(sizeof(LinkNode));
	newnode->Date = x;
	newnode->prev = NULL;
	newnode->next = NULL;
	return newnode;
}
LinkNode* LinkInit(void)
{
	LinkNode* newnode = LinkBuyNode(0);
	newnode->next = newnode;
	newnode->prev = newnode;
	return newnode;
}
void LinkPrint(LinkNode* phead)
{
	LinkNode* cur = phead->next;
	while (cur != phead)
	{
		printf("%d ", cur->Date);
		cur = cur->next;
	}
	printf("\n");
}
void LinkDestory(LinkNode* phead)
{
	LinkNode* next = phead->next;
	LinkNode* prev = phead;
	while (next!=phead)
	{
		prev = next;
		next = next->next;
		free(prev);
	}
	free(phead);
}
void LinkPushBack(LinkNode* phead, L_Datype x)
{
	assert(phead);
	LinkNode* newnode = LinkBuyNode(x);
	LinkNode* first = phead->prev;
	first->next = newnode;
	newnode->prev = first;
	newnode->next = phead;
	phead->prev = newnode;
}
void LinkPushFront(LinkNode* phead, L_Datype x)
{
	assert(phead);
	LinkNode* newnode = LinkBuyNode(x);
	LinkNode* sec = phead->next;
	phead->next = newnode;
	newnode->prev = phead;
	sec->prev = newnode;
	newnode->next = sec;
}
void LinkPopBack(LinkNode* phead)
{
	LinkNode* cur = phead->prev;
	LinkNode* first = cur->prev;
	first->next = phead;
	phead->prev = first;
	free(cur);
	cur = NULL;
}
void LinkPopFront(LinkNode* phead)
{
	LinkNode* cur = phead->next;
	LinkNode* sec = cur->next;
	phead->next = sec;
	sec->prev = phead;
	free(cur);
	cur = NULL;
}
LinkNode* LinkFind(LinkNode* phead, L_Datype x)
{
	LinkNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->Date == x)
		{
			return cur;
		}
		else
			cur = cur->next;
	}
	return NULL;
}
void LinkInert(LinkNode* pos, L_Datype x)
{
	assert(pos);
	LinkNode* prev = pos->prev;
	LinkNode* newnode = LinkBuyNode(x);
	prev->next = newnode;
	newnode->prev = prev;
	pos->prev = newnode;
	newnode->next = pos;
}
void LinkErase(LinkNode* pos)
{
	assert(pos);
	LinkNode* prev = pos->prev;
	LinkNode* next = pos->next;
	prev->next = next;
	next->prev = prev;
}